﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Configuration;
using System.Xml;
using System.Web;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
using System.Web.UI;
using System.Net;
using DreamBuilder.UI.UserControls.DataAccess.Services;
using DreamBuilder.Core.Container;

namespace DreamBuilder.UI.UserControls
{
    /// <summary>
    /// Description:文件上传方法类
    /// Author:张盼
    /// Date:2010-01-16
    /// </summary>
    public class AttachmentUploadFile
    {
        /// <summary>
        /// 数据请求基类
        /// </summary>
        private HttpPostedFile _HPFile = null;
        /// <summary>
        /// 网站根目录
        /// </summary>
        private string _webPath = System.Web.HttpContext.Current.Server.MapPath("~/");
        public AttachmentUploadFile()
        {
            //默认上传路径
            this.Path = System.Web.HttpContext.Current.Server.MapPath("~/");
            //当前时间用作文件名(年,月,日,时,分,秒,3位毫秒)
            this.NewFileName = DateTime.Now.ToString("yMdhhmmssfff");
        }
        #region 公共属性
        /// <summary>
        /// 上传路径 默认:网站根目录
        /// </summary>
        public string Path { get; set; }
        ///<summary>
        /// 新的文件名 默认:年 月 日 时 分 秒 毫秒
        ///</summary>
        public string NewFileName { get; set; }

        /// <summary>
        /// 数据库连接字符串
        /// </summary>
        public string AttachmentDBHelperString { get; set; }

        #endregion
        #region 上传
  
        /// <summary>
        /// 开始上传
        /// </summary>
        /// <param name="hpfBase">数据请求基类</param>
        /// <param name="savePath">要保存的路径</param>
        /// <param name="oldFileName">旧文件名称,便于删除(注:如果存在文件夹路径,程序将自动去除,只留下文件名)</param>
        /// <param name="state">上传状态.  0:上传成功.  1:没有选择要上传的文件.  2:上传文件类型不符.   3:上传文件过大  -1:应用程序错误.</param>
        /// <returns>文件名</returns>
        public string SaveFile(HttpPostedFile hpfBase, ref int state,string attachmentID,DateTime uploadTime,Hashtable addInfos)
        {
            this._HPFile = hpfBase;
            return this.Save(ref state, attachmentID, uploadTime, addInfos);
        }

        /// <summary>
        /// 上传核心
        /// 返回文件名或上传状态
        /// </summary>
        /// <param name="inputFile">上传控件</param>
        /// <param name="savePath">存放的文件夹   从网站根目录起</param>
        /// <param name="oldFileName">在修改图片时指定旧图片名以便删除   默认为空字符串</param>
        /// <returns>返回上传状态.  0:上传成功.  1:没有选择要上传的文件.  2:上传文件类型不符. 3:上传文件过大  -1:应用程序错误.</returns>
        private string Save(ref int state, string attachmentID, DateTime uploadTime, Hashtable addInfos)
        {
            try
            {
                #region 判断是否已选择文件  如果没选择返回旧图片名
                //_inputFileValue为空   表示没有选择文件   返回状态1
                if (this._HPFile.ContentLength == 0)
                {
                    //设置状态为没有选择图片
                    state = 1;
                    //返回旧图片名
                    return string.Empty;
                }
                #endregion
     
                string extension = System.IO.Path.GetExtension(this._HPFile.FileName).ToLower();
                string documentType = extension.Remove(0, 1);
                //存放文件路径+文件名
                string filePath = string.Format("{0}{1}/{2}", this.Path, this.NewFileName, extension);
                //开始上传文件
                if (this._HPFile != null)
                {
                    //this._HPFile.SaveAs(filePath);
                    Stream inputStream = this._HPFile.InputStream;
                    int intFileSize = this._HPFile.ContentLength;
                    Byte[] fileByteArray = new Byte[intFileSize];
                    inputStream.Read(fileByteArray,0,intFileSize);
                    inputStream.Close();
                    AttachmentDAL attachmentDAL = UContainer.Resolve<AttachmentDAL>();
                    attachmentDAL.AttachmentDBHelperString = AttachmentDBHelperString;
                    attachmentDAL.SaveAttachment(this._HPFile.FileName, fileByteArray, attachmentID, uploadTime, documentType, addInfos);
                }
                //设置状态为0,表示上传成功
                state = 0;
                //返回文件名
                return string.Format("{0}{1}", this.NewFileName, extension);
            }
            catch
            {
                //发生严重错误,设置状态为-1,返回旧文件名
                state = -1;
                return string.Empty;
            }
        }

        #endregion
    }
}





